<template>
  <view class="container">
    <!-- 顶部信息区域 -->
    <view class="top-section" style="background-color: #E5E6F4;">
      <view class="user-info">
        <image class="avatar" src="https://bigironcollision.obs.cn-east-3.myhuaweicloud.com:443/temp%2Ftx_1757241357597.png"  mode="aspectFill"></image>
		
        <view class="info-text">
          <text class="name">{{userInfo.storeName}}</text>
          <text class="account">账号: {{userInfo.realname}}</text>
        </view>
      </view>
     
    </view>

    <!-- 团队数据标题 -->
    <view class="section-title">
      <text>导出表格</text>
    </view>
	
	
    <view class="teamDataBox">
		
		 
		 <!-- 时间选择 -->
		 <view class="time-selector">
		   <picker mode="date" @change="bindStartDateChange">
		     <view class="time-item">
		       <text>开始时间: {{startDate}}</text>
		       <uni-icons type="arrowdown" size="16"></uni-icons>
		     </view>
		   </picker>
		   <picker mode="date" @change="bindEndDateChange">
		     <view class="time-item">
		       <text>结束时间: {{endDate}}</text>
		       <uni-icons type="arrowdown" size="16"></uni-icons>
		     </view>
		   </picker>
		 </view>
		 <view class="applyLeader"  @click="daochu2">点击复制到手机浏览器导出</view>
	</view>
 
  </view>
</template>

<script>
export default {
  data() {
    return {
      activeTab: 0,
      startDate: '点击选择时间',
      endDate: '点击选择时间',
      memberList: [],
	  userId:'',
	  userInfo:'',
	  teamFlag:false,
	  userLevel:'',
	  endTime:'',
	  startTime:'',
	  applyLeaderName:'',
	  applyLeaderPhone:'',
	  xls:'',
	  url:'',
    }
  },
  onLoad() {
  	this.userInfo= uni.getStorageSync("userInfo")
	//this.isLeader()
	//this.exportXlsReturnUrlByParams()
	
  },
  methods: {
	exportXlsReturnUrlByParams(){
		this.$http('jeecg-boot/app/market/bicOrder/exportXlsReturnUrlByParams',{
				 storeId:this.userInfo.storeId,//1675431937'
				 startTime:this.startTime,
				 endTime:this.endTime,
		},'GET').then(res => {
			this.xls=res.result
			this.copy()
		 })
	},
	copy(){
		console.log(this.$request.baseUrl)
		var url=this.$request.baseUrl+'jeecg-boot/app/market/bicOrder/downloadTempFile?fileName='+this.xls
		console.log(url)
		uni.setClipboardData({
		  data: url,
		  success: () => {
		    uni.showToast({
		      title: '复制成功'
		    });
		  }
		});
	},
	daochu2(){
		this.exportXlsReturnUrlByParams()
		/* this.$http('jeecg-boot/app/market/bicOrder/downloadTempFile',{
				 fileName:this.xls//this.userInfo.storeId,
		},'GET').then(res => {
			
		 }) */
		/*  var token= uni.getStorageSync("token")
		 uni.downloadFile({
		 	url: 'http://m65c68a7.natappfree.cc/jeecg-boot/app/market/bicOrder/downloadTempFile?fileName='+this.xls, //仅为示例，并非真实的资源
		 	header:{
				"x-access-token":token
			},
			success: (res) => {
		 		if (res.statusCode === 200) {
		 			console.log('下载成功');
		 		}
		 	}
		 }); */
	},
	isLeader(){
		var that=this;
		this.$request.httpBinary('jeecg-boot/app/market/bicOrder/exportXlsByParams',{
				   storeId:this.userInfo.storeId,
				  
		},'GET').then(res => {
			console.log(res)
			 const fs = uni.getFileSystemManager(); // 获取文件系统管理器
			 const filePath = wx.env.USER_DATA_PATH + '/temp.xlsx'; // 指定临时文件路径
			  fs.writeFile({
			         filePath: filePath,
			         data: res,
			         encoding: 'binary', // 写入二进制数据
			         success: () => {
			           // 文件写入成功后，保存到本地
			           uni.saveFile({
			             tempFilePath: filePath,
			             success: (saveRes) => {
			               const savedFilePath = saveRes.savedFilePath;
			               console.log('文件保存成功', savedFilePath);
						   that.saveToDisk(savedFilePath)
			               // 提示用户保存成功，并询问是否打开
			             /*  uni.showModal({
			                 title: '提示',
			                 content: '文件下载成功，是否打开？',
			                 success: (modalRes) => {
			                   if (modalRes.confirm) {
			                     // 打开文档
			                     uni.openDocument({
			                       filePath: savedFilePath,
			                       fileType: 'xlsx',
			                       success: function (openRes) {
			                         console.log('打开文档成功');
			                       },
			                       fail: function (err) {
			                         console.error('打开文档失败', err);
			                       }
			                     });
			                   }
			                 }
			               }); */
			             },
			             fail: (err) => {
			               console.error('保存文件失败', err);
			             }
			           });
			         },
			         fail: (err) => {
			           console.error('写入文件失败', err);
			         }
			       });
		})
	},
	 // 将文件保存到手机磁盘（仅微信小程序支持）
	                saveToDisk(filePath) {
	                    // 注意：此API仅微信小程序支持，且需要用户授权
	                    if (uni.saveFileToDisk) {
	                        uni.saveFileToDisk({
	                            filePath: filePath,
	                            success: () => {
	                                uni.showToast({
	                                    title: '已保存到手机',
	                                    icon: 'success'
	                                });
	                            },
	                            fail: (error) => {
	                                uni.showToast({
	                                    title: '保存失败',
	                                    icon: 'none'
	                                });
	                                console.error('保存到手机失败:', error);
	                            }
	                        });
	                    } else {
	                        uni.showToast({
	                            title: '当前环境不支持',
	                            icon: 'none'
	                        });
	                    }
	                },
    bindStartDateChange(e) {
      this.startDate = e.detail.value
	  this.startTime= this.startDate+' 00:00:00'
	  //this.getTeamMemberList()
    },
    bindEndDateChange(e) {
      this.endDate = e.detail.value
	  this.endTime= this.endDate+' 23:59:59'
	  //this.getTeamMemberList()
    },
	daochu(){
		/* if(!this.applyLeaderPhone || !this.applyLeaderPhone){
			uni.showToast({
			 title: '请填写姓名跟手机号',
			 icon:'none',
			 duration: 3000
			});
			
			return false
		} */
		
	}
  }
}
</script>

<style scoped >
.container {
  display: flex;
  flex-direction: column;
  background-color: #f8f8f8;
  min-height: 100vh;
}

.top-section {
  padding: 20px;
  display: flex;
  border-radius: 0 0 15px 15px;
  justify-content: space-between;
}

.user-info {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  margin-right: 15px;
}

.info-text {
  display: flex;
  flex-direction: column;
}

.name {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 5px;
}

.account {
  font-size: 14px;
  color: #666;
}

.team-rule {
  background-color: #fff;
  padding: 10px;
  border-radius: 8px;
}

.rule-title {
  font-weight: bold;
  display: block;
  margin-bottom: 5px;
}

.rule-content {
  font-size: 14px;
  color: #666;
}

.section-title {
  padding: 15px;
  font-size: 18px;
  font-weight: bold;
  border-bottom: 1px solid #eee;
}

.tabs {
  display: flex;
  padding: 10px 15px;
  border-bottom: 1px solid #eee;
}

.tab-item {
  flex: 1;
  text-align: center;
  padding: 8px 0;
  color: #666;
}

.tab-item.active {
  color: #007AFF;
  border-bottom: 2px solid #007AFF;
}

.time-selector {
  display: flex;
  padding: 15px;
  justify-content: space-between;
}

.time-item {
  display: flex;
  align-items: center;
  background-color: #fff;
  padding: 8px 12px;
  border-radius: 5px;
  font-size: 14px;
}

.member-count {
  padding: 10px 15px;
  font-size: 14px;
  color: #666;
}

.member-list {
  background-color: #fff;
  margin: 10px;
  border-radius: 8px;
}

.list-header {
  display: flex;
  padding: 12px 15px;
  font-weight: bold;
  border-bottom: 1px solid #eee;
}

.list-header text {
  flex: 1;
  text-align: center;
}

.member-item {
  display: flex;
  padding: 12px 15px;
  border-bottom: 1px solid #f5f5f5;
}

.member-item text {
  flex: 1;
  text-align: center;
}
.applyLeader{
	background: linear-gradient(to right, #FF2442, #FF6B81);
	color: #fff;
	padding: 15rpx 40rpx;
	border-radius: 50rpx;
	font-size: 32rpx;
	font-weight: bold;
	width: 600rpx;
	margin: 0 auto;
	text-align: center;
	margin-top: 30rpx;
}
.line{
		display: flex;
		padding: 30rpx 20rpx;
		border-bottom: 1px solid #eee;
		color: #333333;
	}
	.input-field{
		padding-left: 50rpx;
		
	}
.input-label{
	width: 120rpx;
}
</style>